package cn.zn.modules.money.cur.controller;

import cn.zn.framework.common.utils.R;
import cn.zn.modules.money.cur.entity.TMoneyInfoDetail;
import cn.zn.modules.money.cur.service.TMoneyInfoDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("money/cur/moneyinfodetail")
@Api(tags = "经费管理接口")
public class TMoneyInfoDetailController {
    @Autowired
    private TMoneyInfoDetailService tMoneyInfoDetailService;

    @PostMapping
    public R list(@RequestBody Map<String, Object> params) {
        return R.ok().put("page", tMoneyInfoDetailService.queryPage(params));
    }

    @GetMapping("{id}")
    public R info(@PathVariable String id) {
        return R.ok().put("info", tMoneyInfoDetailService.queryDetail(id));
    }

    @PostMapping("save")
    public R save(@RequestBody TMoneyInfoDetail tMoneyInfoDetail) {
        tMoneyInfoDetailService.save(tMoneyInfoDetail);
        return R.ok();
    }

    @PostMapping("update")
    public R update(@RequestBody TMoneyInfoDetail tMoneyInfoDetail) {
        tMoneyInfoDetailService.updateById(tMoneyInfoDetail);
        return R.ok();
    }

    @DeleteMapping
    public R delete(@RequestBody String[] ids) {
        tMoneyInfoDetailService.deleteEntity(Arrays.asList(ids));
        return R.ok();
    }

    @PutMapping("audit/agree")
    public R auditAgree(@RequestBody String[] id) {
        tMoneyInfoDetailService.saveAudit(Arrays.asList(id), true);
        return R.ok();
    }

    @PutMapping("audit/notagree")
    public R auditNotagree(@RequestBody String[] id) {
        tMoneyInfoDetailService.saveAudit(Arrays.asList(id), false);
        return R.ok();
    }

    @PutMapping("active")
    public R active(@RequestBody String[] id) {
        tMoneyInfoDetailService.saveActive(Arrays.asList(id));
        return R.ok();
    }

    /**
     * 上传预算导入文件
     *
     * @param file
     * @return
     */
    @PostMapping("/importDetail")
    @ApiOperation("上传预算导入文件")
    public R importDetail(@RequestParam("file") MultipartFile file) {
        tMoneyInfoDetailService.importDetail(file);
        return R.ok();
    }

    /**
     * 获取未匹配的项目
     *
     * @return
     */
    @GetMapping("/noProjects")
    @ApiOperation("获取未匹配的项目")
    public R noProjects() {
        List<String> list = tMoneyInfoDetailService.queryNoProject();
        return R.ok().put("data", list);
    }

    /**
     * 保存所有未匹配的项目
     *
     * @return
     */
    @PostMapping("/saveProjectAll")
    @ApiOperation("保存所有未匹配的项目")
    public R saveProjectAll() {
        tMoneyInfoDetailService.saveProjectAll();
        return R.ok();
    }

    @PostMapping("/saveAll")
    @ApiOperation("保存导入的经费")
    public R saveAll() {
        tMoneyInfoDetailService.saveAll();
        return R.ok();
    }
}
